

class LocalStorageManager {
    private static instance: LocalStorageManager = null;

    private localData: any = {};

    private isInit: boolean = false;

    public static get inst(): LocalStorageManager {
        if (!this.instance) {
            this.instance = new LocalStorageManager();
        }
        return this.instance;
    }

    private init(): void {
        if (!this.isInit) {
            this.isInit = true;
            this.load();
        }
    }
    public load(): void {

        const localData: string = cc.sys.localStorage.getItem("GameData");

        if (!localData || localData === "") {
            this.localData = {};
        }
        else {
            this.localData = JSON.parse(localData);
        }
    }

    public setValue(key: string, value: any): void {
        this.init();
        this.localData[key] = value;
        this.save();
    }

    public getValue(key: string): any {
        this.init();
        return this.localData[key];
    }
    public save(): void {
        cc.sys.localStorage.setItem("GameData", JSON.stringify(this.localData));
    }

    public clear(): void {
        this.localData = null;
        this.save();
    }
}

const LocalStorageMgr: LocalStorageManager = LocalStorageManager.inst;
export default LocalStorageMgr;